package com.quectel.core.module.group.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quectel.core.module.group.dao.SysGroupVillageDao;
import com.quectel.core.module.group.dto.SysGroupVillageDto;
import com.quectel.core.module.group.entity.SysGroupVillageEntity;
import com.quectel.core.module.group.service.SysGroupVillageService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.Snowflake;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author rananxin
 * @date 2019-08-14 15:22:11
 */
@DubboService
public class SysGroupVillageServiceImpl implements SysGroupVillageService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysGroupVillageServiceImpl.class);

    @Autowired
    private SysGroupVillageDao sysGroupVillageDao;


    @Override
    public List<SysGroupVillageDto> queryList(Map<String, Object> params) {
        List<SysGroupVillageEntity> list = sysGroupVillageDao.queryList(params);
        return CopyUtils.copyList(list, SysGroupVillageDto.class);
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return sysGroupVillageDao.queryTotal(params);
    }

    @Override
    public Long save(SysGroupVillageDto dto) {
        SysGroupVillageEntity entity = CopyUtils.copyObj(dto, SysGroupVillageEntity.class);
        entity.setId(Snowflake.nextId());
        sysGroupVillageDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(SysGroupVillageDto dto) {
        SysGroupVillageEntity entity = CopyUtils.copyObj(dto, SysGroupVillageEntity.class);
        sysGroupVillageDao.updateById(entity);
    }

    @Override
    public SysGroupVillageDto selectById(Long id) {
        SysGroupVillageEntity entity = sysGroupVillageDao.selectById(id);
        return CopyUtils.copyObj(entity, SysGroupVillageDto.class);
    }

    @Override
    public void deleteById(Long id) {
        sysGroupVillageDao.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }

    @Override
    public void deleteByGroupId(Long groupId) {
        sysGroupVillageDao.delete(new QueryWrapper<SysGroupVillageEntity>().eq("sys_group_id", groupId));
    }

    @Override
    public List<Long> queryListByVillageId(Map<String, Object> params) {
        return sysGroupVillageDao.queryListByVillageId(params);
    }

    @Override
    public void deleteByGroupIdAndVillageId(Long groupId, Long objId) {
        sysGroupVillageDao.delete(new QueryWrapper<SysGroupVillageEntity>()
                .eq("sys_group_id", groupId).eq("village_id", objId));
    }

    @Override
    public List<SysGroupVillageDto> queryListByGroupId(Long groupId) {
        List<SysGroupVillageEntity> sysGroupVillageEntities = sysGroupVillageDao.selectList(new QueryWrapper<SysGroupVillageEntity>().eq("sys_group_id", groupId));
        return CopyUtils.copyList(sysGroupVillageEntities, SysGroupVillageDto.class);
    }
}
